#include "includes.h"

int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	HWND hWnd;

	// Initialize PureGDK by telling it where the engine dll is
	if (!initPureGDK("gdkengine.dll","pureGDK"))
		return 1;

	int screenW = GetSystemMetrics(SM_CXSCREEN);
    int screenH = GetSystemMetrics(SM_CYSCREEN);

	// Create a window
	hWnd = openWindow(0, 0, screenW, screenH, "Musicade",
		WS_POPUP | WS_BORDER | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, true);

	// Attach the PureGDK screen to the window
	dbOpenScreen(hWnd, 0, 0, screenW, screenH);

	// turn v-sync on, otherwise it's VERY jittery.
	dbSetDisplayMode(screenW, screenH, 32, 1);

	// Show the window
	ShowWindow(hWnd, SW_SHOW);

	Menu();

	dbCloseScreen();

	return 0;
}

inline bool MouseOver(unsigned short spriteID)
{
    //if sprite exists, and the mouse is inside the sprite bounds, return 1, else 0
    return ( dbIsSprite(spriteID)
            && dbMouseX() >= dbSpriteX(spriteID)
            && dbMouseX() <= dbSpriteX(spriteID) + dbSpriteWidth(spriteID)
            && dbMouseY() >= dbSpriteY(spriteID)
            && dbMouseY() <= dbSpriteY(spriteID) + dbSpriteHeight(spriteID) );
}

float timerBased(float value, bool resetTimer)
{
    // initialise static var, keeps the time of the last loop iteraion
    static unsigned int lastTime;
    // if reset timer is true (explicitly called, defaulted to false) then update teh time for the iteration
    if (resetTimer)
    {
        lastTime = dbTimer();
        return 0.0;
    }
    else
    {
        // compare how many seconds it's been since last loop iteration
        return value * ((dbTimer() - lastTime)/1000.0);
    }
}

int findFreeSprite(void)
{
    // get random number between 1 and 2000, check if that sprite exists, if it does, regenerate number until it doesn't, return that val
    short spriteID(1);
    while (dbIsSprite(spriteID))
    {
        spriteID=dbRND(1999)+1;
    }
    return spriteID;
}
